﻿using System;
using System.Collections.Generic;

using aier.dataaccess.model;
using aier.dataaccess.dapper;
using aier.xrm.wcf;

using Microsoft.Xrm.Sdk;
using log4net;

namespace aier.business.logic
{
    public class ClassBll
    {
        public ClassDal classDal { get; set; }

        public ILog log { get; set; }
        public CommonWcf commonWcf { get; set; }

        /// <summary>
        /// 通过视图获取所有班级信息
        /// </summary>
        /// <returns></returns>
        public List<ClassView> QueryAllClass()
        {
            return classDal.QueryAllClass();
        }

        /// <summary>
        /// 根据班级ID查询视图信息
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public ClassView QueryClassById(string classId)
        {
            //return new CommunityDal().QueryCommunityById(communityId);
            return classDal.QueryClassId(classId);
        }

        /// <summary>
        /// 根据班级名称查询视图信息
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public List<ClassView> QueryClassByName(string provice, string city, string county, string className, string hospital, int stage = 0, Guid? schoolid = null)
        {
            city = city == "全部" ? string.Empty : city;
            county = county == "全部" ? string.Empty : county;
            return classDal.QueryClassByProviceCityCounty(provice, city, county, className, hospital, stage, schoolid);
        }

        /// <summary>
        /// 根据省市区/县ID查询班级视图信息
        /// </summary>
        /// <param name="communityId"></param>
        /// <returns></returns>
        public List<ClassView> QueryClassByProviceCityCounty(string provice, string city, string county, string hospital)
        {
            city = city == "全部" ? string.Empty : city;
            county = county == "全部" ? string.Empty : county;
            return classDal.QueryClassByProviceCityCounty(provice, city, county, null, hospital);
        }

        /// <summary>
        /// 根据省市区县名称，学段(可空)查询班级视图信息
        /// </summary>
        /// <param name="provice">省</param>
        /// <param name="city">市</param>
        /// <param name="county">区</param>
        /// <param name="stage">学段</param>
        /// <returns></returns>
        public List<ClassView> QueryClassByPCAandStage(string provice, string city, string county, string hospital, int stage = 0, Guid? schoolid = null)
        {
            city = city == "全部" ? string.Empty : city;
            county = county == "全部" ? string.Empty : county;
            return classDal.QueryClassByPCAandStage(provice, city, county, hospital, stage, schoolid);
        }
        /// <summary>
        /// 根据社区ID修改社区信息
        /// </summary>
        /// <param name="communityJson"></param>
        /// <returns></returns>
        public ResResult UpdateClassById(ClassInfo classInfo)
        {
            ResResult resResult = new ResResult();
            try
            {
                Entity entity = XrmSqlBuilderEx.AsCrmEntity<ClassInfo>(classInfo);
                //if (classInfo.userid == Guid.Empty || classInfo.userid == null)
                //{
                commonWcf.UpdateEntityById(entity);
                //}
                //else
                //{
                //    commonWcf.UpdateEntityProxyById(classInfo.userid, entity);
                //}
                resResult = new ResResult { ResultState = 1, ResultMes = "Success" };
                return resResult;
            }
            catch (Exception ex)
            {
                log.Error("ClassBll对象中，UpdateClassById方法:" + ex.Message + ex.StackTrace);
                resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = ex.Message };
                return resResult;
            }

            //new CommunityWcf().UpdateCommunityById(entity);
        }

        /// <summary>
        /// 创建班级
        /// </summary>
        /// <param name="entity"></param>
        public ResResult CreateClass(ClassInfo classInfo)
        {
            ResResult resResult = new ResResult();
            if (classInfo == null)
            {
                log.Error("ClassBll对象中，CreateClass方法，classInfo为空");
                resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = "classInfo为空" };
                return resResult;
            }
            if (string.IsNullOrWhiteSpace(classInfo.new_name))
            {
                log.Error("ClassBll对象中，CreateClass方法，new_name为空");
                resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = "new_name为空" };
                return resResult;
            }
            if (classInfo.new_school == Guid.Empty)
            {
                log.Error("ClassBll对象中，CreateClass方法，new_school为空");
                resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = "new_school为空" };
                return resResult;
            }
            if (classInfo.new_grade == 0)
            {
                log.Error("ClassBll对象中，CreateClass方法，new_grade");
                resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = "new_grade为空" };
                return resResult;
            }
            //if (string.IsNullOrWhiteSpace(classInfo.new_classnumber))
            //{
            //    log.Error("ClassBll对象中，CreateClass方法，new_classnumber为空");
            //    resResult = new ResResult { ResultState = 0, ResultMes = "Fail", Result = "new_classnumber为空" };
            //    return resResult;
            //}
            try
            {
                Entity entity = XrmSqlBuilderEx.AsCrmEntity<ClassInfo>(classInfo);
                Guid targetid = Guid.Empty;
                if (classInfo.userid == Guid.Empty || classInfo.userid == null)
                {
                    targetid = commonWcf.CreateEntity(entity);
                }
                else
                {
                    targetid = commonWcf.CreateEntityProxy(classInfo.userid, entity);
                }
                resResult = new ResResult { ResultState = 1, ResultMes = "Success", Result = targetid };
                return resResult;
            }
            catch (Exception ex)
            {
                log.Error("ClassBll对象中，CreateClass方法:" + ex.StackTrace + ex.Message);
                resResult = new ResResult { ResultState = 1, ResultMes = "Fail", Result = ex.Message };
                return resResult;
            }

            //return new CommunityWcf().CreateCommuntity(entity);
        }

        /// <summary>
        /// 根据班级信息和医院ID获取班级列表
        /// </summary>
        /// <param name="classinfo"></param>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public List<ClassView> getClassListByConditions(ClassView classinfo, Guid hospitalId)
        {
            List<ClassView> classViews = null;
            classViews = classDal.getClassListByConditions(classinfo, hospitalId);
            return classViews;
        }

        /// <summary>
        /// 根据学校ID或班级名称查询视图信息
        /// 原程序 ：creeningServiceImpl-> queryClass(String schoolId, String className)
        /// </summary>
        /// <param name="schoolId">学校ID</param>
        /// <param name="classname">班级名称</param>
        /// <returns></returns>
        public IEnumerable<ClassView> QueryClassBySchoolIdAndClassName(Guid schoolId, string classname)
        {
            return classDal.QueryClassBySchoolIdAndClassName(schoolId, classname);
        }
    }
}
